<?php
/*
 *后台用户配置
 *author 王建 
 */

class SysadminAction extends BaseAction{
	public $d = '';
	function __construct(){
		parent::__construct();
		$this->d = D("Admin/Sysadmin");;
	}
	function index(){
		$action = $this->_request("action");	
		$action_array = array("show","ajax_data");
		$action = !in_array($action,$action_array)?'show':$action ;
		if($action == 'show'){
			$group = $this->d->getGroup();
			$this->assign("group" , $group) ;
			$this->display();
		}elseif($action == 'ajax_data'){
			$this->ajax_data();
		}
	}
	//ajax get data
	private function ajax_data(){
		$page = intval($this->_request("page"  ));
		$list = $this->d->querySysadminList($page ,50 , 
				array(
							'username'=>trim($this->_request("username")) ,
							'gid'=>trim($this->_request("gid")) ,
							'nick'=>trim($this->_request("nick")) ,
						 	'condition'=>$this->_request("condition"  ) ,
						 	'field'=>$this->_request("field"  ) ,
							'orderby'=>$this->_request("orderby"  ) ,
						) );
		echo result_to_towf_new($list['list'], 1, '成功', $list['page_sting']) ;
	}
	//添加后台用户
	function add(){
		
		$action = $this->_request("action");		
		$action_array = array("add","doadd");
		$action = !in_array($action,$action_array)?'add':$action ;	
		if($action == 'add'){
			$list = D("Admin/Role")->getRole();
			$this->assign("list" , $list) ;
			$this->display();		
		}elseif($action =='doadd'){
			$this->doadd();
		}
	}
	//处理添加
	private function doadd(){
		$data = array(
			'gid'=>	intval($this->_request("gid" ))	,
			'username'=>trim($this->_request("username" ) ),
			'nick'=>trim($this->_request("nick" ) ),
			'passwd'=>$this->_request("passwd"  )  , 
			'status'=>intval($this->_request("status"  ))	,
			'super_admin'=>	intval($this->_request("super_admin"  )) , 
			'addtime'=>date("Y-m-d H:i:s",time()),
		);
		$data = $this->d->add_sysadmin($data);
		$url = U("sysadmin/index") ;
		if($data['status']){
			echo "<script>parent.window.location.href='{$url}';</script>";
		}else{
			throw_exception($data['message']);
			exit ;
		}	
	}
	//编辑页面
	function edit(){
		$action = $this->_request("action");		
		$action_array = array("edit","doedit");
		$action = !in_array($action,$action_array)?'edit':$action ;		
		if($action == 'edit'){
			$id = intval($this->_request("id"));//用户的ID
			/*if($this->admin_id == $id ){
				showmessage("对不起你不能编辑自己的权限信息","sys_admin/index",3,0);
				exit();
			}*/
			$result = $this->d->judge($id);
			if(!$result['status']){
				throw_exception($result['message']);
				exit;
			}
			/* echo "<pre>";
			print_r($result); */
			$this->assign("options" , $result['options']) ;
			$this->assign("info" , $result['info']) ;
			$this->assign("list" , $result['list']) ;
			$this->assign("perm_array_exists" , $result['perm_array_exists']) ;
			$this->display();		

		}elseif($action == 'doedit'){
			$this->doedit();
		}

	}
	//处理编辑数据
	private function doedit(){
		
		$data = array(
			'id'=>	$this->_request("id"  ) , 
			'nick'=>trim($this->_request("nick"  )) ,
			'super_admin'=> $this->_request("super_admin") , 
			'status'=>$this->_request("status") , 
			'gid'=>$this->_request("gid") ,
			'perms'=>$this->_request("p") , 
			
		);
		
		$data = $this->d->doSetPerminsions($data);
		if($data['status'] == 0 ){
			$this->error($data['message']) ;
			exit();
		}
		$this->success("修改成功" ,U("sysadmin/index")  ) ;
		
	}
	//管理员修改密码
	public function passwd(){
		$action = $this->_request("action");
		$action_array = array("edit","doedit");
		$action = !in_array($action,$action_array)?'passwd':$action ;
		if($action == 'passwd' ){
			$id = intval($this->_request("id"))  ;
			$info = $this->d->getInfoById($id);
			$this->assign("info" , $info) ;
			$this->display();
		}elseif($action == 'doedit' ){
			$id = intval($this->_request("id"  ))  ;
			$password = $this->_request("password" )  ; 
			$data = array(
				'id'=>$id , 
				'passwd'=>$password		
			);
			$data = $this->d->edit_passwd($data);
			if($data['status'] == 0 ){
				throw_exception($data['message']) ; 
				exit();
			}
			$url = U("sysadmin/index") ;
			echo "<script>parent.window.location.href='{$url}';</script>";
		}
		
	}
	
}